﻿@model IList<ProductSpecificationModel>
@using Nop.Web.Models.Catalog;
@using System.Xml;
@if (Model.Count > 0)
{
    //Product details spesicification
    //updated by <tianyingchun>
    <div class="product-specs-box">
        @*@T("Products.Specs")*@
        <strong>Quick Details</strong>
        @{var grouped = Model.GroupBy(s => s.SpecificationAttributeName);
        var xmlDoc = new XmlDocument();
        xmlDoc.LoadXml("<table><tr></tr></table>");
        var curNode = xmlDoc.DocumentElement.LastChild;
        bool isFirst = true;
        int colIndex = 0;
        foreach (var sa in grouped)
        {
            if (!isFirst && colIndex % 4 == 0)
            {
                colIndex = 0;
                XmlElement tr = xmlDoc.CreateElement("tr");
                xmlDoc.DocumentElement.AppendChild(tr);
                curNode = xmlDoc.DocumentElement.LastChild;
            }
            //1. add specificationattributename as first td.
            XmlElement td = xmlDoc.CreateElement("td");
            td.SetAttribute("class", "sa-name");
            td.InnerXml = string.Format("<span>{0}:</span>", sa.Key);
            curNode.AppendChild(td);
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            var isColor = sa.Key.Equals("color", StringComparison.InvariantCultureIgnoreCase);
            foreach (var sao in sa)
            {
                var saoName = sao.ValueRaw;
                if (isColor)
                {
                    var saoValues = sao.ValueRaw.Split('-');
                    saoName = saoValues[0];
                    // sb.Append(string.Format("<span title='{0}' class=\"color\" style='background-color:{1}'></span>", saoName, saoValues[1]));
                }
                sb.Append(HttpUtility.HtmlDecode(saoName));
                sb.Append(", ");
            }
            XmlElement td2 = xmlDoc.CreateElement("td");
            td2.SetAttribute("class", "sa-value");
            string savalue = sb.ToString().Substring(0, sb.ToString().Length - 2);
            td2.InnerText = savalue.ToStringLength(30);
            td2.SetAttribute("title", savalue);
            curNode.AppendChild(td2);
            colIndex++;
            isFirst = false;
        }
        for (int i = 0; i < 4 - colIndex; i++)
        {
            XmlElement tdholderName = xmlDoc.CreateElement("td");
            XmlElement tdholderValue = xmlDoc.CreateElement("td");
            curNode.AppendChild(tdholderName);
            curNode.AppendChild(tdholderValue);
        }
        }
        @Html.Raw(xmlDoc.InnerXml)
    </div>
}
